package com.david.appletree;

import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

//参考：https://www.runoob.com/w3cnote/android-tutorial-xml.html
class HtmlSaxHelper extends DefaultHandler {
   static String str = "<div class=\"tag\" w=\"40\" h=\"30\" style=\"font-family: SimHei; font-size: 12px; background: rgb(255, 255, 255); width: 151.181px; height: 103.386px; position: relative; margin: 2px 0px 3px;\">         <div class=\"item\" style=\"position: absolute; overflow: hidden; border: 1px solid transparent; top: 45.9375px; left: 15.9271px; height: 48.8203px; line-height: 22px; width: 109.828px; cursor: default;\" id=\"0\" val=\"bc\">{{条型码}}</div><div class=\"item\" style=\"position: absolute; overflow: hidden; border: 1px solid transparent; top: 3.92709px; left: 9.90625px; height: 20px; line-height: 20px; cursor: default;\" id=\"1\" val=\"text\">{{款号}}</div><div class=\"item\" style=\"position: absolute; overflow: hidden; border: 1px solid transparent; top: 23.9271px; left: 9.90625px; height: 20px; line-height: 20px; cursor: default;\" id=\"2\" val=\"text\">{{颜色}}</div><div class=\"item\" style=\"position: absolute; overflow: hidden; border: 1px solid transparent; top: 23.9271px; left: 82.9062px; height: 20px; line-height: 20px; cursor: default;\" id=\"3\" val=\"text\">{{尺码}}</div></div>";
//   public static void main(String[] args) {
//
//      //①创建XML解析处理器
//      HtmlSaxHelper ss = new HtmlSaxHelper();
//      //②得到SAX解析工厂
//      SAXParserFactory factory = SAXParserFactory.newInstance();
//      //③创建SAX解析器
//      SAXParser parser = null;
//
//      StringReader reader = new StringReader(str);
//
//      try {
//         parser = factory.newSAXParser();
//         //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器
//         parser.parse(new InputSource(reader), ss);
//      } catch (ParserConfigurationException e) {
//         e.printStackTrace();
//      } catch (SAXException e) {
//         e.printStackTrace();
//      } catch (IOException e) {
//         e.printStackTrace();
//      }
//
//   }

   private HtmlSaxHelper.HtmlElement element;
   private ArrayList<HtmlSaxHelper.HtmlElement> elements;
   //当前解析的元素标签
   private String tagName = null;

   /**
    * 当读取到文档开始标志是触发，通常在这里完成一些初始化操作
    */
   @Override
   public void startDocument() throws SAXException {
      this.elements = new ArrayList<>();
      Log.i("SAX", "读取到文档头,开始解析xml");
   }


   /**
    * 读到一个开始标签时调用,第二个参数为标签名,最后一个参数为属性数组
    */
   @Override
   public void startElement(String uri, String localName, String qName,
                            Attributes attributes) throws SAXException {
//      if (localName.equals("person")) {
//         person = new HtmlSaxHelper.Person();
//         person.setId(Integer.parseInt(attributes.getValue("id")));
//         Log.i("SAX", "开始处理person元素~");
//      }
      if ("div".equals(localName)) {
         element = new HtmlElement();

         element.setName("div");

         HashMap<String, String> attrs = new HashMap<>();
         for (int i = 0; i < attributes.getLength(); i++) {
            attrs.put(attributes.getLocalName(i), attributes.getValue(i));
         }
         element.attrs = attrs;
//         int width = Integer.parseInt(attributes.getValue("w"));
//         int height = Integer.parseInt(attributes.getValue("h"));
//         String style = attributes.getValue("style");
//         String[] attrs = style.split(";");
//         for (String item:attrs) {
//            if ("color".equals(item)) {
//               //字体颜色
//            }
//         }
      }
      this.tagName = localName;
   }


   /**
    * 读到到内容,第一个参数为字符串内容,后面依次为起始位置与长度
    */

   @Override
   public void characters(char[] ch, int start, int length)
           throws SAXException {
      //判断当前标签是否有效
      if (this.tagName != null) {
         String data = new String(ch, start, length);
         //读取标签中的内容
         if (this.tagName.equals("div")) {
            this.element.setContent(data);
            Log.i("SAX", "处理name元素内容");
         }
      }

   }

   /**
    * 处理元素结束时触发,这里将对象添加到结合中
    */
   @Override
   public void endElement(String uri, String localName, String qName)
           throws SAXException {
      if (localName.equals("div")) {
         this.elements.add(element);
//         person = null;
//         Log.i("SAX", "处理person元素结束~");
      }
      this.tagName = null;
   }

   /**
    * 读取到文档结尾时触发，
    */
   @Override
   public void endDocument() throws SAXException {
      super.endDocument();
      Log.i("SAX", "读取到文档尾,xml解析结束");
   }

   //获取persons集合
   public ArrayList<HtmlSaxHelper.HtmlElement> getPersons() {
      return elements;
   }

   public void parse(){
      //①创建XML解析处理器
      HtmlSaxHelper ss = new HtmlSaxHelper();
      //②得到SAX解析工厂
      SAXParserFactory factory = SAXParserFactory.newInstance();
      //③创建SAX解析器
      SAXParser parser = null;

      StringReader reader = new StringReader(str);

      try {
         parser = factory.newSAXParser();
         //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器
         parser.parse(new InputSource(reader), ss);
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } catch (SAXException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   static class HtmlElement{
      String name;
      String content;
      HashMap<String, String> attrs;


      public HashMap<String, String> getAttrs() {
         return attrs;
      }

      public void setAttrs(HashMap<String, String> attrs) {
         this.attrs = attrs;
      }

      public String getName() {
         return name;
      }

      public void setName(String name) {
         this.name = name;
      }

      public String getContent() {
         return content;
      }

      public void setContent(String content) {
         this.content = content;
      }
   }
}
